/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine - graphics
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AERenderLoopHelper_ogl.cpp
// Author:		Gianluca Belardelli
// Date:		15/06/2015
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

AERenderLoopHelper::AERenderLoopHelper( void )
{
}

AERenderLoopHelper::~AERenderLoopHelper( void )
{
}

void AERenderLoopHelper::ClearScreen( AEUINT32 uiBufferFlags, AEColor colClearColor, AEFLOAT32 fDepthRef, AEINT32 nStencilRef, AEINT32 nRTMask ) const
{
	glClearColor( 1.0f, 1.0f, 0.0f, 0.0f );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	//glBindFramebuffer(GL_FRAMEBUFFER, 0);
	//glDrawBuffer( GL_COLOR_ATTACHMENT0 );

}

void AERenderLoopHelper::PerformHardwareOcclusionQuery( void )
{
}

void AERenderLoopHelper::PerformHardwarePixelCounterQuery( void )
{
}

/*#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
*/
void AERenderLoopHelper::RenderStaticGeometryWithShader( const AEStaticGeometryInstanceCollection &refGeoInstances, AECompiledShaderPass &refShader )
{
	AEINT32 nGeometries = refGeoInstances.GetNumEntries();
	
	if( nGeometries == 0 )
		return;

	AERenderContext *lpContext = AERenderContext::GetCurrentContext();

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	AEMatrix4f matProj = lpContext->GetProjectionMatrix();
	GLint programID;
	glGetIntegerv (GL_CURRENT_PROGRAM,&programID );

	GLfloat fMatrix[16];

	matProj.Get( fMatrix, AEML_RowMajor );
	GLuint MatrixID = glGetUniformLocation( programID, "MatProj" );

	glUniformMatrix4fv( MatrixID, 1, GL_FALSE, (GLfloat *)&fMatrix );
	//glUniformMatrix4fv( MatrixID, 1, GL_FALSE, (GLfloat *)&view );
	//GLuint uiSampler; 
	GLenum glErr = GL_NO_ERROR;
	/*glGenSamplers( 1, &uiSampler );
	glErr = glGetError();
	glSamplerParameteri(uiSampler, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glErr = glGetError();
	glSamplerParameteri(uiSampler, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glErr = glGetError();*/
	for( AEINT32 i=0; i<nGeometries; i++ )
	{
		AEStaticGeometryInstance *lpGeom = refGeoInstances.GetEntry( i );
		AEStaticSubmeshInstance *lpSubMesh = (AEStaticSubmeshInstance *)refGeoInstances.GetEntry( i );
		
		GLuint matModel = glGetUniformLocation( programID, "MatModel" );
		glErr = glGetError();
		GLfloat fMatrix2[16];

		lpSubMesh->GetMeshInstance()->GetTransform().Get( fMatrix2, AEML_RowMajor );
		glUniformMatrix4fv( matModel, 1, GL_FALSE, (GLfloat *)&fMatrix2 );
		glErr = glGetError();
		glActiveTexture(GL_TEXTURE0);
		glErr = glGetError();
		AETexture *lpPtr = lpSubMesh->GetMaterial()->GetBaseTextureObject();
		GLuint tex = lpPtr->GetDeviceHandle();

		//unsigned char *buffer = (unsigned char*)calloc(1600000, 1);
		//glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_RGB, buffer);


		GLuint sampler = glGetUniformLocation( programID, "texDiffuse" );
		glErr = glGetError();
		glBindTexture( GL_TEXTURE_2D, tex );

		glErr = glGetError();
		//glBindSampler( 0, uiSampler );
		glUniform1i ( sampler, 0 );
		glErr = glGetError();

		lpGeom->Render();
	}

	//glDeleteSamplers( 1, &uiSampler );
}

void AERenderLoopHelper::RenderStaticGeometrySurfaceShaders( const AEStaticGeometryInstanceCollection &refGeoInstances, AEPassType ePassType )
{
	AEINT32 nGeometries = refGeoInstances.GetNumEntries();

	for( AEINT32 i=0; i<nGeometries; i++ )
	{
		AEStaticGeometryInstance *lpGeom = refGeoInstances.GetEntry( i );
		lpGeom->Render();
	}
}
